using System;
using System.Collections.Generic;
using System.Text;

namespace MonoCash.ObjectModel
{
    class Price : ObjectModel
    {

        public static DateTime LastPriceDate(Security s)
        {
            DateTime d = DateTime.MinValue;
            try
            {
                d = DateTime.Parse(Program.AppSettings.Data.Prices.Compute("Max(Date)", "SecurityID = '" + s.ID + "'").ToString());
            }
            catch { }
            return d;
        }
        public static Decimal GetPrice(Security s, DateTime date)
        {
            Decimal temp = 1;
            try
            {
                string filter = "Date <= '" + date.ToLongDateString() + "' and SecurityID=" + s.ID.ToString();
                DateTime bestDate = (DateTime)Program.AppSettings.Data.Prices.Compute("Max(Date)", filter);
                filter = filter.Replace("<=", "=");
                filter = filter.Replace(date.ToLongDateString(), bestDate.ToLongDateString());

                temp = (Decimal)Program.AppSettings.Data.Prices.Select(filter)[0]["Price"];
            }
            catch { }
            return temp;
        }

        private Decimal lastPrice;

        public Decimal LastPrice
        {
            get { return lastPrice; }
            set { lastPrice = value; SyncToBackend(); }
        }

        private DateTime date = DateTime.Today;
        public DateTime Date
        {
            get { return date; }
            set { date = value; SyncToBackend(); }
        }

        private DateTime entryDate = DateTime.Now;
        public DateTime EntryDate
        {
            get { return entryDate; }
            set { entryDate = value; SyncToBackend(); }
        }

        private int securityID = int.Parse(Program.AppSettings.DefaultSecurityID);
        public int SecurityID
        {
            get { return securityID; }
            set { securityID = value; SyncToBackend(); }
        }

        public Security Security
        {
            get { return new Security(SecurityID); }
            set { SecurityID = value.ID; }
        }
	
	
	
    }
}
